package com.itic.mall.timer.jdproduct;

import java.util.Date;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itic.appbase.framework.common.constants.Constants;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.utils.IdGen;
import com.itic.appbase.framework.utils.NumberStringHelper;
import com.itic.appbase.framework.utils.RandomStringHelper;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.framework.utils.UserSessionHelper;
import com.itic.mall.mobile.base.BaseMobileInterface;
import com.itic.mall.mobile.jd.MallJdUtils;
import com.itic.mall.system.productjd.persistence.model.ProductJd;
import com.itic.mall.system.productjd.persistence.model.ProductJdExample;

public class JdProductTimer extends BaseMobileInterface {

    	 /**
         * 递归同步商品列表
         * 
         * @param pageIndex
         *            起始页码
         * @param pageSize
         *            页大小
         */
        public void execute(){
        	//deleteAll();
        	Integer total = 36000;//总数量30000条
        	//每次同步1条数据
        	for( int j=0;j<total;j++ ){
        		 JSONArray productList = MallJdUtils.queryProductList(j, 1);
        		 for (int i = 0; i < productList.size(); i++) {       	
        	            JSONObject p = productList.getJSONObject(i);
    	            	//判断数据库有没有这个数据
    	            	ProductJd jd = productJdService.getBySku(p.getString("sku"));
    	            	
    	            	/*String convert = MallJdUtils.convert("0", p.getString("sku"), p.getString("conUrlB"));
    	            	//判断能否生成二合一链接
    	            	if( null != convert ){}*/
    	            	

	            		if( null == jd ){
    	            		ProductJd model = new ProductJd();
        	                model.setId(IdGen.uuid());
        	                ProductJdExample example = new ProductJdExample();
        	                example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE);
        	                int count = productJdService.count(example);
        	                model.setReorder(count+1);
        	                model.setCreateTime(new Date());
        	                model.setUpdateTime(new Date());
        	                model.setCreateUser(null != UserSessionHelper.get(Constants.USER_ID) ? UserSessionHelper.get(Constants.USER_ID) : "unknown");
        	                model.setOrgCode(null != UserSessionHelper.get(Constants.USER_ORG) ? UserSessionHelper.get(Constants.USER_ORG) : "unknown");
        	                model.setIsEffective(DBConst.TRUE);
        	                model.setProDesc(p.getString("proDesc"));
        	                model.setJdId(p.getString("jdId"));
        	                model.setCategoryName(p.getString("categoryName"));
        	                model.setGoodsInventory(p.getString("goodsInventory"));
        	                model.setParentId(p.getString("parentId"));
        	                model.setConExpireDate(p.getString("conExpireDate"));
        	                model.setOriPrice(p.getString("oriPrice"));
        	                model.setConUrl(p.getString("conUrl"));
         	                model.setGoodsUrl(p.getString("goodsUrl"));
        	                model.setConTicketPrice(p.getString("conTicketPrice"));
        	                
        	                model.setCompetitive(p.getString("competitive"));
        	                model.setRawId(p.getString("id"));
        	                model.setSku(p.getString("sku"));
        	                model.setConUrlB(p.getString("conUrlB"));
        	                model.setGoodsName(p.getString("goodsName"));
        	                model.setConPrice(p.getString("conPrice"));
        	                model.setTotalSales(RandomStringHelper.randomNumStr(4));
        	                
        	                /*if("1".equals(model.getCompetitive())){
        	                	model.setIsBaokuan("001");
        	                	ProductJdExample example2 = new ProductJdExample();
            	                example2.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andIsBaokuanEqualTo("001");
            	                int count2 = productJdService.count(example2);
        	                	model.setOrderBaokuan(count2+1);
        	                }*/
        	                
        	                /**
        	                 * 这是拿到手的佣金，京东还没有扣百分之十的
        	                 */
        	                // String subtractString = NumberStringHelper.subtractString(p.getString("oriPrice"), p.getString("conTicketPrice"));
        	                String mulString = NumberStringHelper.mulString(p.getString("conPrice"), p.getString("commission"));
        	                String profit = NumberStringHelper.divString(mulString, "100", 2);
        	                
        	                String dianzhuCommission = getDianzhuCommission(profit);
        	                //佣金除以百分之
        	                model.setCommissionProfit(dianzhuCommission);
        	                String dianzhuCommissionPercent = getDianzhuCommissionProfit(dianzhuCommission,p.getString("conPrice"));
        	                //计算店主的佣金 比
        	                model.setCommission(dianzhuCommissionPercent);
        	                productJdService.add(model);
        	                
        	                System.out.println("成功同步了京东商品"+model.getGoodsName());
    	            	}else{
    	            		//重新算一下店主和利润和百分比
    	            		/**
        	                 * 这是拿到手的佣金，京东还没有扣百分之十的
        	                 */
        	                //String subtractString = NumberStringHelper.subtractString(p.getString("oriPrice"), p.getString("conTicketPrice"));
        	                String mulString = NumberStringHelper.mulString(p.getString("conPrice"), p.getString("commission"));
        	                String profit = NumberStringHelper.divString(mulString, "100", 2);
    	            		
    	            		String dianzhuCommission = getDianzhuCommission(profit);
    	            		String dianzhuCommissionPercent = getDianzhuCommissionProfit(dianzhuCommission,p.getString("conPrice"));
    	            		jd.setCommission(dianzhuCommissionPercent);
    	            		jd.setCommissionProfit(dianzhuCommission);
    	            		productJdService.edit(jd);
    	            	}
	            	
        	        }
        	}
            
        }
        
        /**
         * 计算店主的佣金
         * @return
         */
        public String getDianzhuCommission(String commission){
        	//京东扣除百分之十
        	String jdPercent="10";
        	String byKey = sysSettingService.getByKey("JD_MARKET_PROFIT_PERCENT");
        	if( StringHelper.isNotBlank(byKey) ){
        		jdPercent=byKey;
        	}
        	String divString = NumberStringHelper.divString(jdPercent, "100", 2);
        	String jdProfit = NumberStringHelper.mulString(commission, divString);//结算到京东的利润
        	
        	String backProfit = NumberStringHelper.subtractString(commission, jdProfit);//这是京东返回给我们的利润
        	
        	//与店主平分下利润
        	String ydProfit = NumberStringHelper.divString(backProfit, "1.8", 2);//这是给店主的利润
        	
        	return ydProfit;
        }
        
        /**
         * 计算店主的佣金百分比后多少金额
         * @return
         */
        public String getDianzhuCommissionProfit(String dianzhuCommission,String conPrice ){
        	String divString = NumberStringHelper.divString(dianzhuCommission, conPrice, 2);
        	String dianzhuPercent = NumberStringHelper.mulString(divString, "100");
        	return dianzhuPercent;
        }
}